/*
 * AllowedLayersListPanel.java
 *
 * Created on May 25, 2011, 10:23:27 AM
 */
package com.iceberg.adgt.ui.gen;

import com.iceberg.adgt.common.ControllerException;
import com.iceberg.adgt.common.DomainException;
import com.iceberg.adgt.common.Observer;
import com.iceberg.adgt.common.ObserverInterest;
import com.iceberg.adgt.domain.DomainFacade;
import com.iceberg.adgt.domain.DomainFacadeImpl;
import com.iceberg.adgt.ui.architecture.ArchitectureController;
import com.iceberg.adgt.ui.architecture.LayerListModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author David
 */
public class AllowedLayersListPanel extends javax.swing.JPanel implements Observer {

	/**
	 * Creates new form AllowedLayersListPanel
	 */
	public AllowedLayersListPanel() {
		initComponents();
		init();
	}

	/**
	 * This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jScrollPane1 = new javax.swing.JScrollPane();
		allowedLayerList = new javax.swing.JList();
		deleteButton = new javax.swing.JButton();

		jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
		jLabel1.setText("Allowed To Communicate With");

		allowedLayerList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
		allowedLayerList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
			public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
				allowedLayerListValueChanged(evt);
			}
		});
		jScrollPane1.setViewportView(allowedLayerList);

		deleteButton.setText("Delete");
		deleteButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				deleteButtonActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
										.addComponent(jLabel1)
										.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
										.addComponent(deleteButton))
								.addContainerGap())
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jLabel1)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(deleteButton))
		);
	}// </editor-fold>//GEN-END:initComponents

	private void allowedLayerListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_allowedLayerListValueChanged
	}//GEN-LAST:event_allowedLayerListValueChanged

	private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
		if (controller != null && allowedLayerList.getSelectedIndex() != -1) {
			try {
				controller.removeAllowedLayer(listModel.getLayerAt(allowedLayerList.getSelectedIndex()));
			} catch (DomainException ex) {
				LOG.error("Unable to remove allowed layer", ex);
			} catch (ControllerException ex) {
				LOG.error("No selection is made", ex);
			}
		}
	}//GEN-LAST:event_deleteButtonActionPerformed

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JList allowedLayerList;
	private javax.swing.JButton deleteButton;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JScrollPane jScrollPane1;
	// End of variables declaration//GEN-END:variables
	private static final Logger LOG = LoggerFactory.getLogger(AllowedLayersListPanel.class);
	private ArchitectureController controller;
	private LayerListModel listModel;

	@Override
	public final void notifyChange(ObserverInterest interest) {
		this.setAllowedLayers();
	}

	public final void setController(ArchitectureController controller) {
		this.controller = controller;
		this.controller.addObserver(this, ObserverInterest.LAYER_SELECTION_CHANGED);
	}

	private void setAllowedLayers() {
		try {
			listModel = new LayerListModel(this.controller.getAllowedLayersForLayer());
			allowedLayerList.setModel(listModel);
			allowedLayerList.setEnabled(true);

		} catch (ControllerException ex) {
			this.allowedLayerList.setModel(new LayerListModel());
			this.allowedLayerList.setEnabled(false);
		}
	}

	private void init() {
		DomainFacade facade = DomainFacadeImpl.getInstance();
		facade.addObserver(this, ObserverInterest.LAYERS_CHANGED);
	}
}
